package ylsb.service;


import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ylsb.dao.Hc2Mapper;
import ylsb.dao.Hc45Mapper;
import ylsb.dao.ShijiMapper;
import ylsb.model.*;
import ylsb.support.JsonResult;
import ylsb.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Hc45Service {

    @Resource
    private Hc45Mapper ddMapper;

    private static int startindex = 0;

    public List<Map<String, Object>> findPinzhongAll(String ppxiangmuId){
        return ddMapper.findPinzhongAll(ppxiangmuId);
    }

    public List<Map<String, Object>> findGuochanPinzhong(String ppxiangmuId){
        return ddMapper.findGuochanPinzhong(ppxiangmuId);
    }
//    public List<Map<String, Object>> findFengbi(String ppxiangmuId){
//        return ddMapper.findFengbi(ppxiangmuId);
//    }
//    public List<Map<String, Object>> findFengbiCount(String ppxiangmuId,String ppPinzhong){
//        return ddMapper.findFengbiCount(ppxiangmuId,ppPinzhong);
//    }

    public List<Map<String, Object>> findGuochanBypinzhong(String ppxiangmuId,String ppPinzhong){
        return ddMapper.findGuochanBypinzhong(ppxiangmuId,ppPinzhong);
    }
    public List<Map<String, Object>> findPinpaiList(String ppxiangmuId,int ppPinzhong){
        return ddMapper.findPinpaiList(ppxiangmuId,ppPinzhong);
    }
    public List<Map<String, Object>> findGuochanmuluBypinzhong(String ppxiangmuId,String ppPinzhong){
        return ddMapper.findGuochanmuluBypinzhong(ppxiangmuId,ppPinzhong);
    }
    public List<Map<String, Object>> findResultBypinzhong(String ppxiangmuId, String ppuserId,String ppPinzhong){
        return ddMapper.findResultBypinzhong(ppxiangmuId,ppuserId,ppPinzhong);
    }

    public List<Map<String, Object>> findResultGuochanBypinzhong(String ppxiangmuId, String ppuserId,String ppPinzhong){
        return ddMapper.findResultGuochanBypinzhong(ppxiangmuId,ppuserId,ppPinzhong);
    }

//    public List<Map<String, Object>> findAllByXiangmuIdAndTiaojian(String xiangmuId,String mmleibie,String mmpinpaiqingkuang){
//        return ddMapper.findAllByXiangmuIdAndTiaojian(xiangmuId,mmleibie,mmpinpaiqingkuang);
//    }

    public List<Map<String, Object>> findAllByXiangmuIdAndTiaojian(String xiangmuId){
        return ddMapper.findAllByXiangmuIdAndTiaojian(xiangmuId);
    }

    public List<Map<String, Object>> findXinghaoByPinpai(String ppxiangmuId,int ppPinzhong,String ppPinpai){
        return ddMapper.findXinghaoByPinpai(ppxiangmuId,ppPinzhong,ppPinpai);
    }

    public Integer getPinguiCount(String mmxiangmuId){
        Object mmCount = ddMapper.getPinguiCount(mmxiangmuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }
    public Integer getPinzhongCount(String mmxiangmuId){
        Object mmCount = ddMapper.getPinzhongCount(mmxiangmuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }
    public Integer getPinpaiCount(String mmxiangmuId){
        Object mmCount = ddMapper.getPinpaiCount(mmxiangmuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }
    public Integer getPaichuPinguiCount(String mmxiangmuId){
        Object mmCount = ddMapper.getPaichuPinguiCount(mmxiangmuId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Hc45 findOne(String id){
        return ddMapper.selectByPrimaryKey(id);
    }

    public List<Map<String,Object>> getCountByPinzhongAndPinpai(int mmPinzhong,String mmPinpai,String mmxiangmuId){
        return ddMapper.getCountByPinzhongAndPinpai(mmPinzhong,mmPinpai,mmxiangmuId);
    }

    public JsonResult<?> findhc45list(String ppxiangmuId, int ppPageIndex, int ppPageSize) {

        List<Hc45pinzhong> pinzhongs = ddMapper.selectAllByXiangmuid(ppxiangmuId, (ppPageIndex - 1) * ppPageSize, ppPageSize);
        List<Map<String,Object>> hc45List = new ArrayList<>();
        for (Hc45pinzhong pinzhong : pinzhongs) {
            Map<String, Object> pinzhongMap = new HashMap<>();
            List<Map<String,Object>> returnPinpaiList = new ArrayList<>();
            int pinzhonghebinghangshu = 0;
            for (Hc45pinpai pinpai : pinzhong.getHc45pinpaiList()) {
                Map<String, Object> pinpaiMap = new HashMap<>();
                pinpaiMap.put("pinpaixuhao", pinpai.getPinpaixuhao());
                int size = pinpai.getHc45xinghaoList().size();
                pinpaiMap.put("pinpaihebinghang", size == 0 ? 1 : size);
                pinpaiMap.put("xinghaolist", pinpai.getHc45xinghaoList());
                returnPinpaiList.add(pinpaiMap);
                pinzhonghebinghangshu += size;
            }
            pinzhongMap.put("yinjinleixing", pinzhong.getYinjinleixing());
            pinzhongMap.put("pinzhongming", pinzhong.getPinzhongming());
            pinzhongMap.put("pinzhongxuhao", pinzhong.getPinzhongxuhao());
            pinzhongMap.put("pinpailist", returnPinpaiList);
            pinzhongMap.put("pinzhonghebinghang", pinzhonghebinghangshu == 0 ? pinzhong.getHc45pinpaiList().size() : pinzhonghebinghangshu);
            hc45List.add(pinzhongMap);
        }
        int count = getHc45list(ppxiangmuId, ppPageIndex, ppPageSize);
        PageInfo pageInfo = new PageInfo(ppPageIndex, ppPageSize, count);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("Hc45List", hc45List);
        returnMap.put("PageInfo", pageInfo);

        return JsonResult.getSuccessResult(returnMap);
    }

    public JsonResult<?> findBiaojuelist(@RequestParam("xiangmuid") String ppxiangmuId,
                                         @RequestParam("userid") String ppuserId,
//                                         @RequestParam("tiaojian") String pptiaojian,
                                         @RequestParam("pageindex") int ppPageIndex,
                                         @RequestParam("pagesize") int ppPageSize) {
//        String mmleibie="";
//        String mmpinpaiqingkuang="";
//        if(pptiaojian.equals("封闭试剂")){
//            mmleibie=pptiaojian;
//        }else if(pptiaojian.equals("一个品牌")){
//            mmpinpaiqingkuang=pptiaojian;
//        }else if(pptiaojian.equals("两个品牌")){
//            mmpinpaiqingkuang=pptiaojian;
//        }else if(pptiaojian.equals("专机专用")){
//            mmpinpaiqingkuang=pptiaojian;
//        }


//        List<Hc45pinzhong> pinzhongAll = ddMapper.selectBiaojueByXiangmuid(ppxiangmuId, ppuserId, 0, 9999);
        List<Map<String,Object>> hc45List = new ArrayList<>();

//        int startindex = 0;
//        if (ppPageIndex != 1){
//            for (int i = 0; i < (ppPageIndex - 1) * ppPageSize; i++) {
//                if (i >= pinzhongAll.size())
//                    break;
//                for (Hc45pinpai pinpai : pinzhongAll.get(i).getHc45pinpaiList()) {
//                    startindex += pinpai.getHc45xinghaoList().size();
//                }
//            }
//        }
//        int pageSize = 0;
//        for (int i = (ppPageIndex - 1) * ppPageSize; i < ppPageIndex * ppPageSize; i++) {
//            if (i >= pinzhongAll.size())
//                break;
//            for (Hc45pinpai pinpai : pinzhongAll.get(i).getHc45pinpaiList()) {
//                pageSize += pinpai.getHc45xinghaoList().size();
//            }
//        }
        int startIndex = (ppPageIndex - 1) * ppPageSize;
        List<Hc45pinzhong> pinzhongPage = ddMapper.selectBiaojueByXiangmuid(ppxiangmuId, ppuserId, startIndex, ppPageSize);


        for (Hc45pinzhong pinzhong : pinzhongPage) {
            Map<String, Object> pinzhongMap = new HashMap<>();
            List<Map<String,Object>> returnPinpaiList = new ArrayList<>();
            int pinzhonghebinghangshu = 0;
            String pinzhongresult = "0";

            for (Hc45pinpai pinpai : pinzhong.getHc45pinpaiList()) {
                String pinpairesult = "0";
                for (Hc45xinghao xinghao : pinpai.getHc45xinghaoList())
                    if ("1".equals(xinghao.getResult())) {
                        pinpairesult = "1";
                        pinzhongresult = "1";
                    }
                Map<String, Object> pinpaiMap = new HashMap<>();
                pinpaiMap.put("pinpaixuhao", pinpai.getPinpaixuhao());
                pinpaiMap.put("shiyongyuanzeid", pinpai.getShiyongyuanzeid());
                pinpaiMap.put("pinpairesult", pinpairesult);
                int size = pinpai.getHc45xinghaoList().size();
                pinpaiMap.put("pinpaihebinghang", size == 0 ? 1 : size);
                pinpaiMap.put("xinghaolist", pinpai.getHc45xinghaoList());
                returnPinpaiList.add(pinpaiMap);
                pinzhonghebinghangshu += size;
            }
            pinzhongMap.put("yinjinleixing", pinzhong.getYinjinleixing());
            pinzhongMap.put("pinzhongming", pinzhong.getPinzhongming());
            pinzhongMap.put("pinzhongxuhao", pinzhong.getPinzhongxuhao());
            pinzhongMap.put("pinzhongresult", pinzhongresult);
            pinzhongMap.put("pinpailist", returnPinpaiList);
            pinzhongMap.put("pinzhonghebinghang", pinzhonghebinghangshu == 0 ? pinzhong.getHc45pinpaiList().size() : pinzhonghebinghangshu);
            hc45List.add(pinzhongMap);
        }
        int count = getHc45list(ppxiangmuId, ppPageIndex, ppPageSize);
        PageInfo pageInfo = new PageInfo(ppPageIndex, ppPageSize, count);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("Hc45List", hc45List);
        returnMap.put("PageInfo", pageInfo);

        return JsonResult.getSuccessResult(returnMap);
    }

    public Integer getHc45list(String ppxiangmuId,int ppPageIndex, int ppPageSize) {
        Object mmCount = ddMapper.getHc45list(ppxiangmuId,ppPageIndex,ppPageSize);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public boolean add (Hc45 hc45){
        return ddMapper.insert(hc45)==1;
    }

    public boolean isexistByxiangmuId(String xiangmuId){
        Hc45Example mmExample=new Hc45Example();
        Hc45Example.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.selectByExample(mmExample).size()>0;
    }

    public boolean deleteByxiangmuId(String xiangmuId){
        Hc45Example mmExample=new Hc45Example();
        Hc45Example.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.deleteByExample(mmExample)>=0;
    }
}
